package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class CheckGroupController {


    @Autowired
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    public Result add(@RequestParam String checkitemIds,
                        @RequestBody CheckGroup checkGroup){
        log.info("添加的检查项Id:" + checkitemIds);

        checkGroupService.add(checkitemIds,checkGroup);

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.ADD_CHECKGROUP_SUCCESS);
        return result;
    }

    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id){

        checkGroupService.deleteById(id);

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        return result;
    }

    /**
     * 根据检查套餐id查询检查组id
     * @param id
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer id){

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        result.setData(checkGroupService.findCheckGroupIdsBySetmealId(id));
        return result;
    }


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查项:" + queryPageBean);

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        result.setData(checkGroupService.findPage(queryPageBean));
        return result;
    }

    @PostMapping("/edit")
    public Result edit(@RequestParam String checkitemIds,
                       @RequestBody CheckGroup checkGroup){

        checkGroupService.edit(checkitemIds,checkGroup);
        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        return result;
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        result.setData(checkGroupService.findAll());
        return result;
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据检查组id查询检查组:" + id);
        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKITEM_SUCCESS);
        result.setData(checkGroupService.findById(id));
        return result;
    }

}
